package com.jisuan.contract.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jisuan.contract.common.utils.PageResult;
import com.jisuan.contract.common.utils.Query;
import com.jisuan.contract.dao.SchoolContractDao;
import com.jisuan.contract.entity.EmployeeEntity;
import com.jisuan.contract.entity.FirstPartyEntity;
import com.jisuan.contract.entity.SchoolContractEntity;
import com.jisuan.contract.service.EmployeeService;
import com.jisuan.contract.service.FirstPartyService;
import com.jisuan.contract.service.SchoolContractService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Slf4j
@Service("schoolContractService")
public class SchoolContractServiceImpl extends ServiceImpl<SchoolContractDao, SchoolContractEntity> implements SchoolContractService {

    @Autowired
    FirstPartyService firstPartyService;

    @Autowired
    EmployeeService employeeService;

    @Override
    public PageResult queryPage(Map<String, Object> params, SchoolContractEntity contract)
    {
        LambdaQueryWrapper<SchoolContractEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(
                contract.getContractName() != null,
                SchoolContractEntity::getContractName, contract.getContractName()
        );
        IPage<SchoolContractEntity> page = this.page(
                new Query<SchoolContractEntity>().getPage(params),
                queryWrapper
        );

        // 封装带 甲方乙方名称 的合同列表数据
        page.getRecords().forEach(schoolContractEntity -> {
            FirstPartyEntity firstPartyEntity = firstPartyService.getById(schoolContractEntity.getFirstPartyId());
            EmployeeEntity employeeEntity = employeeService.getById(schoolContractEntity.getSecondPartyId());

            schoolContractEntity.setFirstPartyName(firstPartyEntity.getFirstPartyName());
            schoolContractEntity.setSecondPartyName(employeeEntity.getEmployeeName());
        });

        return new PageResult(page);
    }
}
